
package net.swa.dewei.web.action;

import java.util.List;

import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.FuwuZhan;
import net.swa.dewei.beans.entity.Merchants;
import net.swa.dewei.service.MerchantsService;
import net.swa.system.beans.entity.Dict;
import net.swa.system.beans.entity.User;
import net.swa.system.service.ICommonService;
import net.swa.system.util.SysConst;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.BuzConst;
import net.swa.util.JsonResult;

/**商家信息表 Action**/
public class MerchantsAction extends AbstractBaseAction
{
    private static final long serialVersionUID = -1541842392065452147L;

    private final Logger log = Logger.getLogger(MerchantsAction.class);

    private MerchantsService merchantsService;

    //实体类id
    private Long id;

    //实体类
    private Merchants model;

    //商家名称
    private String name;

    //商家类型
    private String merchType;

    //商家类型接口
    private List<Dict> merTypes;

    //状态
    private String merchStatus;

    //id集合
    private Long[] ids;

    private Long fuwuZhanId;

    private String fuwuzhanName;

    /** 公用方法接口 */
    private ICommonService commonService;

    private Boolean isAdmin;

    /**初始化分页查询**/
    @Log(description = "初始化商家信息列表")
    public String listPage()
    {
        merTypes = commonService.search("title", BuzConst.MERCHANTS_TITLE, Dict.class, null, "dictPaixu", "asc");
        return "listPage";
    }

    public String queryList() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<Merchants> json = null;
        User user = getCurrentUser();
        if (null != user)
        {
            if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
            {
                fuwuZhanId = user.getFuwuzhanId();
            }
            else
            {
                fuwuZhanId = null;
            }
        }
        json = merchantsService.queryPageList(fuwuZhanId, fuwuzhanName, name, merchType, merchStatus, currentPage, pageSize, orderBy, orderType);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    public String toView() throws Exception
    {
        if (null != id)
        {
            log.debug("");
            model = commonService.commonFind(Merchants.class, id);
            if (null != model)
            {
                if (null != model.getFuwuzhanId())
                {
                    FuwuZhan fuwuzhan = commonService.commonFind(FuwuZhan.class, model.getFuwuzhanId());
                    if (null != fuwuzhan)
                    {
                        model.setFwzName(fuwuzhan.getName());
                    }
                }
            }
        }
        return "toView";
    }

    public String toUpdateDown() throws Exception
    {
        model = commonService.commonFind(Merchants.class, id);
        if (null != model)
        {
            if (null != model.getFuwuzhanId())
            {
                FuwuZhan fuwuzhan = commonService.commonFind(FuwuZhan.class, model.getFuwuzhanId());
                if (null != fuwuzhan)
                {
                    model.setFwzName(fuwuzhan.getName());
                }
            }
        }
        //下架操作（查阅界面）
        return "xiajia";
    }

    @Log(description = "管理员对商家活动信息审阅")
    public String toCheck() throws Exception
    {
        model = commonService.commonFind(Merchants.class, id);
        if (null != model)
        {
            if (null != model.getFuwuzhanId())
            {
                FuwuZhan fuwuzhan = commonService.commonFind(FuwuZhan.class, model.getFuwuzhanId());
                if (null != fuwuzhan)
                {
                    model.setFwzName(fuwuzhan.getName());
                }
            }
        }
        //下架操作（下架界面）
        return "toCheckMerchants";
    }

    /** 去新增或者修改页面 **/
    @Log(description = "去新增或者修商家活动")
    public String toEditMerchants() throws Exception
    {
        merTypes = commonService.search("title", BuzConst.MERCHANTS_TITLE, Dict.class, null, "dictPaixu", "asc");

        log.debug("");
        FuwuZhan fuwuzhan = null;
        User user = getCurrentUser();
        if (null != user)
        {
            if (null != id)
            {
                model = commonService.commonFind(Merchants.class, id);
                if (null != model)
                {
                    if (null != model.getFuwuzhanId())
                    {
                        fuwuzhan = commonService.commonFind(FuwuZhan.class, model.getFuwuzhanId());
                        if (null != fuwuzhan)
                        {
                            model.setFwzName(fuwuzhan.getName());
                        }
                    }
                }
            }
            else
            {
                model = new Merchants();
                if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
                {
                    if (null != user.getFuwuzhanId())
                    {
                        fuwuzhan = commonService.commonFind(FuwuZhan.class, user.getFuwuzhanId());
                        if (null != fuwuzhan)
                        {
                            model.setFwzName(fuwuzhan.getName());
                            model.setFuwuzhanId(fuwuzhan.getId());
                        }
                    }
                }
            }
            return "toAddMerchants";
        }
        else
        {
            return LOGIN;
        }
    }

    @Log(description = "保存商家活动")
    public String saveMerchants() throws Exception
    {
        JsonResult<String> json = new JsonResult<String>();
        if (null == model.getId())
        {
            json = merchantsService.addModel(model);
        }
        else
        {
            json = merchantsService.updateModel(model);
        }
        outJson(json);
        return null;
    }

    @Log(description = "商家活动下架")
    public String saveDownMerchants() throws Exception
    {
        JsonResult<String> json = new JsonResult<String>();
        if (null != model && null != model.getId())
        {
            json = merchantsService.updateCheckModel(model);
            outJson(json);
        }
        else
        {
            outError("传参错误");
        }
        return null;
    }

    @Log(description = "审核商家活动")
    public String saveCheckMerchants() throws Exception
    {
        JsonResult<String> json = new JsonResult<String>();
        if (null != model && null != model.getId())
        {
            json = merchantsService.updateCheckModel(model);
            outJson(json);
        }
        else
        {
            outError("传参错误");
        }
        return null;
    }

    public String batchUpdate() throws Exception
    {
        log.debug("删除信息：" + ids);
        boolean f = merchantsService.batchUpdate(ids);
        if (f)
        {
            log.debug("成功完成删除商家信息");
            outSuccess();
        }
        else
        {
            outError("操作出错");
        }
        return null;
    }

    public MerchantsService getMerchantsService()
    {
        return merchantsService;
    }

    @Required
    @Resource
    public void setMerchantsService(MerchantsService merchantsService)
    {
        this.merchantsService = merchantsService;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Merchants getModel()
    {
        return model;
    }

    public void setModel(Merchants model)
    {
        this.model = model;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Long getFuwuZhanId()
    {
        return fuwuZhanId;
    }

    public void setFuwuZhanId(Long fuwuZhanId)
    {
        this.fuwuZhanId = fuwuZhanId;
    }

    public String getFuwuzhanName()
    {
        return fuwuzhanName;
    }

    public void setFuwuzhanName(String fuwuzhanName)
    {
        this.fuwuzhanName = fuwuzhanName;
    }

    public String getMerchType()
    {
        return merchType;
    }

    public void setMerchType(String merchType)
    {
        this.merchType = merchType;
    }

    public String getMerchStatus()
    {
        return merchStatus;
    }

    public void setMerchStatus(String merchStatus)
    {
        this.merchStatus = merchStatus;
    }

    public Boolean getIsAdmin()
    {
        return isAdmin;
    }

    public void setIsAdmin(Boolean isAdmin)
    {
        this.isAdmin = isAdmin;
    }

    public List<Dict> getMerTypes()
    {
        return merTypes;
    }

    public void setMerTypes(List<Dict> merTypes)
    {
        this.merTypes = merTypes;
    }

}
